/*
 * Copyright (c) 2011-2014, fortiss GmbH.
 * Licensed under the Apache License, Version 2.0.
 *
 * Use, modification and distribution are subject to the terms specified
 * in the accompanying license file LICENSE.txt located at the root directory
 * of this software distribution. A copy is available at
 * http://chromosome.fortiss.org/.
 *
 * This file is part of CHROMOSOME.
 *
 * $Id: readSensorValueFunction.c 7856 2014-03-14 17:09:07Z gulati $
 */

/**
 * \file
 *         Source file for function readSensorValue in component sensorMB.
 *
 * \author
 *         This file has been generated by the CHROMOSOME Modeling Tool (XMT)
 *         (fortiss GmbH).
 */

/******************************************************************************/
/***   Includes                                                             ***/
/******************************************************************************/
#include "sensorMonitor/adv/sensorMB/include/readSensorValueFunction.h"

#include "sensorMonitor/adv/sensorMB/include/readSensorValueFunctionWrapper.h"
#include "sensorMonitor/adv/sensorMB/include/sensorMBComponent.h"
#include "sensorMonitor/adv/sensorMB/include/sensorMBComponentWrapper.h"
#include "sensorMonitor/adv/sensorMB/include/sensorMBManifest.h"

#include "xme/core/logUtils.h"

// PROTECTED REGION ID(SENSORMONITOR_ADV_SENSORMB_READSENSORVALUEFUNCTION_C_INCLUDES) ENABLED START
#include "sensorMonitor/adv/common/include/readSensorValueCommon.h"

#include "xme/hal/include/safeString.h"

#include <inttypes.h>
// PROTECTED REGION END

/******************************************************************************/
/***   Definitions                                                          ***/
/******************************************************************************/

// PROTECTED REGION ID(SENSORMONITOR_ADV_SENSORMB_READSENSORVALUEFUNCTION_C_DEFINITIONS) ENABLED START

// PROTECTED REGION END

/******************************************************************************/
/***   Variables                                                            ***/
/******************************************************************************/
/**
 * \brief  Variable holding the value of the required output port 'sensorValueOut'.
 *
 * \details If necessary initialize this in the init function.
 *          The value of this variable will be written to the port at the end of
 *          the step function.
 */
static sensorMonitor_topic_sensorData_t
portSensorValueOutData;

// PROTECTED REGION ID(SENSORMONITOR_ADV_SENSORMB_READSENSORVALUEFUNCTION_C_VARIABLES) ENABLED START

// PROTECTED REGION END

/******************************************************************************/
/***   Prototypes                                                           ***/
/******************************************************************************/

// PROTECTED REGION ID(SENSORMONITOR_ADV_SENSORMB_READSENSORVALUEFUNCTION_C_PROTOTYPES) ENABLED START

// PROTECTED REGION END

/******************************************************************************/
/***   Implementation                                                       ***/
/******************************************************************************/
xme_status_t
sensorMonitor_adv_sensorMB_readSensorValueFunction_init
(
    sensorMonitor_adv_sensorMB_sensorMBComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(SENSORMONITOR_ADV_SENSORMB_READSENSORVALUEFUNCTION_INITIALIZE_C) ENABLED START

    sensorMonitor_adv_common_fillSensorType(componentConfig);
    sensorMonitor_adv_common_fillSensorHostName(componentConfig);
    sensorMonitor_adv_common_fillSensorTopic(componentConfig);

    return XME_STATUS_SUCCESS;

    // PROTECTED REGION END
}

void
sensorMonitor_adv_sensorMB_readSensorValueFunction_step
(
    sensorMonitor_adv_sensorMB_sensorMBComponent_config_t* const componentConfig
)
{
    xme_status_t status[1];
    
    sensorMonitor_topic_sensorData_t* portSensorValueOutDataPtr = &portSensorValueOutData;
    
    {
        // PROTECTED REGION ID(SENSORMONITOR_ADV_SENSORMB_READSENSORVALUEFUNCTION_STEP_C) ENABLED START

        uint64_t sensorValue = sensorMonitor_adv_common_fillSensorValue(componentConfig);

        // Convert from bytes to MB
        uint64_t sensorValueMB = sensorValue / 1000000;

        // Print current free space to console
        XME_LOG
        (
            XME_LOG_ALWAYS,
            "Current %s on %s is %" PRIu64 " MB (%" PRIu64 " %s)\n",
            componentConfig->sensorType,
            componentConfig->sensorInstance,
            sensorValueMB,
            (sensorValue < 1024) ? sensorValue : (
                (sensorValue < 1024*1024) ? sensorValue/1024 : (
                (sensorValue < 1024*1024*1024) ? sensorValue/1024/1024 : sensorValue/1024/1024/1024)),
            (sensorValue < 1024) ? "Bytes" : (
                (sensorValue < 1024*1024) ? "KiB" : (
                (sensorValue < 1024*1024*1024) ? "MiB" : "GiB"))
        );

        xme_hal_safeString_strncpy(portSensorValueOutData.sensorType, componentConfig->sensorType, sizeof(portSensorValueOutData.sensorType));
        xme_hal_safeString_strncpy(portSensorValueOutData.hostName, componentConfig->hostName, sizeof(portSensorValueOutData.hostName));
        xme_hal_safeString_strncpy(portSensorValueOutData.sensorInstance, componentConfig->sensorInstance, sizeof(portSensorValueOutData.sensorInstance));
        portSensorValueOutData.sensorValue = sensorValueMB;

        // PROTECTED REGION END
    }
    
    status[0] = sensorMonitor_adv_sensorMB_sensorMBComponentWrapper_writePortSensorValueOut(portSensorValueOutDataPtr);
    
    {
        // PROTECTED REGION ID(SENSORMONITOR_ADV_SENSORMB_READSENSORVALUEFUNCTION_STEP_2_C) ENABLED START

        if (XME_STATUS_SUCCESS != status[0])
        {
            XME_LOG ( XME_LOG_ALWAYS, "Write of Sensor Data to port failed\n" );
        }

        // PROTECTED REGION END
    }
}

void
sensorMonitor_adv_sensorMB_readSensorValueFunction_fini
(
    sensorMonitor_adv_sensorMB_sensorMBComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(SENSORMONITOR_ADV_SENSORMB_READSENSORVALUEFUNCTION_TERMINATE_C) ENABLED START

    // Nothing to do
    
    XME_UNUSED_PARAMETER(componentConfig);

    // PROTECTED REGION END
}

// PROTECTED REGION ID(SENSORMONITOR_ADV_SENSORMB_READSENSORVALUEFUNCTION_IMPLEMENTATION_C) ENABLED START

// PROTECTED REGION END
